Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web

<Assembly: WebResource("jObjectiveDotNET.jObjective.js", "text/javascript")> 
<Assembly: WebResource("jObjectiveDotNET.jObjective.debug.js", "text/javascript")> 

Public Class ScriptWriter
    Inherits WebControls.WebControl

    Private m_sbClientEnhancedObjects As New System.Text.StringBuilder()
    Private m_sbThinControlObjects As New Text.StringBuilder
    Private m_ctlScriptManager As New ScriptManager

    Public Const DefaultNamespace As String = "Page"

    Public ReadOnly Property ScriptManager() As ScriptManager
        Get
            Return m_ctlScriptManager
        End Get
    End Property

    Protected Overrides Sub CreateChildControls()
        MyBase.CreateChildControls()

        With m_ctlScriptManager
            .ID = Common.GetUniqueID(Context)
        End With

        Controls.Add(m_ctlScriptManager)

        ChildControlsCreated = True
    End Sub

    Private Sub ScriptWriter_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        EnsureChildControls()
    End Sub

    Private Sub ScriptWriter_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        With m_sbClientEnhancedObjects
            .Append("var ")
            .Append(DefaultNamespace)
            .AppendLine("=new jObjective.DataObject();")
            .Append(Add(Page.Controls, DefaultNamespace))
        End With
    End Sub

    Public Shared Function Add(ByVal Controls As System.Web.UI.ControlCollection, ByVal [Namespace] As String) As Text.StringBuilder
        Dim objRetVal As New Text.StringBuilder

        For Each objControl As Control In Controls
            Dim objWebDataControl As IJObjective = TryCast(objControl, IJObjective)
            If objWebDataControl IsNot Nothing AndAlso Not String.IsNullOrEmpty(objWebDataControl.ObjectName) Then
                Dim sNamespace As String = [Namespace] & "." & objWebDataControl.ObjectName

                With objRetVal
                    .Append(sNamespace)
                    .Append("=")
                    .Append(objWebDataControl.Constructor)
                    .AppendLine(";")
                End With

                objWebDataControl.Namespace = sNamespace

                'Add the child jObjective
                objRetVal.Append(Add(objControl.Controls, sNamespace))
            Else
                'Flatten out non jObjective
                objRetVal.Append(Add(objControl.Controls, [Namespace]))
            End If
        Next

        Return objRetVal
    End Function

    Public Sub Add(ByVal PropertyName As String, ByVal ParentNamespace As String, ByVal ClientID As String)
        With m_sbThinControlObjects
            .Append(ParentNamespace)
            .Append(".")
            .Append(PropertyName)
            .Append("=new jObjective.DataObject($('#")
            .Append(ClientID)
            .Append("'));")
        End With
    End Sub

    Private Sub ScriptWriter_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        If HttpContext.Current.IsDebuggingEnabled Then
            ScriptManager.Scripts.Add(New ScriptReference("jObjectiveDotNET.jObjective.debug.js", "jObjectiveDotNET"))
        Else
            ScriptManager.Scripts.Add(New ScriptReference("jObjectiveDotNET.jObjective.js", "jObjectiveDotNET"))
        End If
        ScriptManager.RegisterStartupScript(Me, Me.GetType, "ClientEnhancedObjects", m_sbClientEnhancedObjects.ToString(), True)
        ScriptManager.RegisterStartupScript(Me, Me.GetType, "ThinControlObjects", m_sbThinControlObjects.ToString(), True)
    End Sub
End Class